package com.ddw.meedeeuh.shared.io;

import java.io.File;

public abstract class PathUtils {

	/**
	 * Gets the current working directory
	 * @return a directory instance
	 */
	public static File getCurrentDirectory() {
		return new File(".");
	}

	/**
	 * Combines two paths into one
	 * @param path1 the first path
	 * @param path2 the second path
	 * @return a combined path
	 */
	public static String combine(String path1, String path2) {
		return new File(new File(path1), path2).getPath();
	}

	/**
	 * Combines two paths into one
	 * @param path1 the first path
	 * @param path2 the second path
	 * @return a combined path
	 */
	public static String combine(File path1, String path2) {
		return new File(path1, path2).getPath();
	}

	/**
	 * Gets the extension of a file path
	 * @param path the path to return the extension of
	 * @return an extension including the . or null if no extension is found
	 */
	public static String getExtension(String path) {
		final int lastIndex = path.lastIndexOf('.');
		return lastIndex == -1 ? null : path.substring(lastIndex);
	}

	/**
	 * Gets the extension of a file
	 * @param file the file to return the extension of
	 * @return an extension including the . or null if no extension is found
	 */
	public static String getExtension(File file) {
		return getExtension(file.getPath());
	}

	public static String changeExtension(String path, String extension) {
		if(extension == null) {
			throw new IllegalArgumentException("The extension parameter cannot be null");
		}
		if(extension.indexOf('.') != 0) {
			throw new IllegalArgumentException("The extension parameter must start with a period");
		}

		final int lastIndex = path.lastIndexOf('.');
		return lastIndex == -1 ? path.concat(extension) : path.substring(0, lastIndex).concat(extension);
	}

	public static String changeExtension(File file, String extension) {
		return changeExtension(file.getPath(), extension);
	}

}
